require("../util.js");
require("../component/components.js");
require("../component/cityChoice.js");
require("../../jsMt/util.js");
var userInfo=JSON.parse(sessionStorage.getItem("userInfo"));
var dataInfo=JSON.parse(sessionStorage.getItem("data"));
if(!userInfo){
    location.href="/loginIndex"
}
var vm = new Vue({
    el:"#editJob",
    data:{
        type:"",
        // loginUid:userInfo.id,
        loginUid:userInfo.id,
        list:{
            recruitId:"",
            enterpriseId :"", //企业id
            title:"",   //，名称
            category:"",  //职位类别 名称
            education:"",//学历
            experience:"",//经验
            nature:"", //工作性质
            introduce:"",//描述
            salary:"",//薪资
            state:"1",// 1上线0下线
            areaId:""//所在地
        },
        listName:{
            educationName:"",//学历
            natureName:"",//工作性质
            areaName:"",//所在地
            salaryName:"",//月收入
            experienceName:"",//工作经验
            introduce:"",//描述标题
            stateName:""//状态
            // hobby:""  //爱好
        },
        isDescription:false,
        message:"家乡",
        isScreen:"",
        dataInfo:dataInfo,
        recruitsInfo:{},
        recruits:{},
        nowYear:"",
        nowMonth:"",
        nowDate:"",
        initId1:"",
        rCategorys:[],
        initId2:"",
        rCategorys1:[],
        introduce:""
    },
    mounted:function(){
        this.searchObject = this.getQueryStringArgs();
        this.type=this.searchObject&&this.searchObject.type;
        this.list.enterpriseId=this.searchObject&&this.searchObject.enterpriseId;
        if(this.searchObject&&this.searchObject.recruitId){
            this.list.recruitId=this.searchObject&&this.searchObject.recruitId;
        }
        if(this.type==2){
            this.getUserInfo();
        }

    },
    methods:{
        //后退
        getBack: function () {
            history.go(-1);
        },
        bgimg:function(url){
            url = url?url:"";
            return {
                "background-image":'url("'+url+'"),url("")'
            }
        },
        /*获取查询字符对象*/
        getQueryStringArgs: function () {
            //取得查询字符串并去掉开头的问号
            var qs = (location.search.length > 0 ? location.search.substring(1) : ""),
                args = {},
                items = qs.length ? qs.split("&") : [],
                item = null,
                name = null,
                value = null,
                len = items.length;
            for (var i = 0; i < len; i++) {
                item = items[i].split("=");
                name = item[0];
                value = item[1];
                if (name.length) {
                    args[name] = value;
                }
            }
            return args;
        },
        //获取职位信息
        getUserInfo:function(){
            var that=this;
            var data={
                loginUid:that.loginUid,
                recruitId:that.list.recruitId
            };
            that.ajax({url:"/recruit/getRecruitInfo",data:data,dataType:"json",method:"post"},function(b){
                if(b.json.code==0&&b.json.data){
                    that.recruits=b.json.data;
                    // that.recruitsInfo.forEach(function(val){
                    //     if(val.recruitId==that.recruitId){
                    //         that.recruits=val;
                    //
                    //     }
                    // });
                    that.list.title=that.recruits.title; //昵称
                    that.list.education=that.recruits.education; //学历
                    that.listName.educationName=that.recruits.educationName;
                    that.list.salary=that.recruits.salary;  //月收入
                    that.listName.salaryName=that.recruits.salaryName;
                    that.list.nature=that.recruits.nature;
                    that.listName.natureName=that.recruits.natureName;
                    that.list.areaId=that.recruits.areaId;  //所在地
                    that.listName.areaName=that.recruits.areaName;
                    that.list.experience=that.recruits.experience;  //工作经验
                    that.listName.experienceName=that.recruits.experienceName;
                    that.list.state=that.recruits.state; //状态
                    that.listName.stateName=that.recruits.stateName;
                    that.list.introduce=that.recruits.introduce; //职位描述
                    that.list.category=that.recruits.category; //类别

                    console.log(that.recruits)
                }else{
                    vm.$refs.prompt.toast("获取个人信息失败！");
                }
            });
        },
        getIsScreen:function(num){
            this.isScreen=num;
        },
        //下拉框选择
        getSelect:function(num){
            var that=this;
            var type;
            if(num==1){  //学历
                type="hk_recruit_education";
            }else if(num==2){   //月收入
                type="hk_recruit_salary";
            }else if(num==3){  //工作性质
                type="hk_recruit_nature";
            }else if(num==4){   //工作经验
                type="hk_recruit_workingLife";
            }
            console.log(type);
            var arr={};
            var data=[];
            that.dataInfo.dict.forEach(function(val,key){
                if(val.type==type){
                    arr={};
                    arr.id=val.id;
                    arr.value=val.label;
                    data.push(arr);
                }
            });
            var bankSelect = new IosSelect(1,
                [data],
                {
                    container: '.provingMode',
                    itemHeight: 30,
                    itemShowCount: 3,
                    callback: function (selectOneObj) {
                        console.log(selectOneObj)
                        if(num==1){  //学历
                            that.list.education=selectOneObj.id;
                            that.listName.educationName=selectOneObj.value;
                        }else if(num==2){   //月收入
                            that.list.salary=selectOneObj.id;
                            that.listName.salaryName=selectOneObj.value;
                        }else if(num==3){  //性质
                            that.list.nature=selectOneObj.id;
                            that.listName.natureName=selectOneObj.value;
                        }else if(num==4){   //工作经验
                            that.list.experience=selectOneObj.id;
                            that.listName.experienceName=selectOneObj.value;
                        }
                    }
                });
        },
        //城市选择
        choiceList:function(data){
            console.log(data)
            this.list.areaId=data.areasId;
            this.listName.areaName=data.areasName;
        },
        //是否上下线
        getState:function(){
            if(this.list.state==1){
                this.list.state=0;
            }else{
                this.list.state=1;
            }
        },
        //职业选择
        getOccupation:function(name){
            this.list.category=name;
            // this.userInfo.occupation=name;
            this.rCategorys=[];
            this.rCategorys1=[];
            this.initId1="";
            this.initId2="";
            this.getIsScreen();
        },
        //职位描述
        getDescription:function(num){
            this.isDescription=!this.isDescription;
            if(num){
                this.introduce="岗位职位:";
            }
        },
        recruitCategorys:function(id,type){
            var that=this;
            if(type==1){
                that.rCategorys=[];
                that.rCategorys1=[];
                that.initId1=id;
            }else if(type==2){
                that.rCategorys1=[];
                that.initId2=id;
            }
            that.dataInfo.recruitCategorys.forEach(function(item,index){
                if(item.parentId==id){
                    if(type==1){
                        that.rCategorys.push(item);
                    }else if(type==2){
                        that.rCategorys1.push(item);
                    }
                }
            });
            // console.log(that.rCategorys);
        },
        //保存
        updateUserData:function(){
            var that=this;
            if(!that.list.title){
                vm.$refs.prompt.toast("名称不能为空！");
                return false;
            }
            if(!that.list.category){
                vm.$refs.prompt.toast("职位类别不能为空！");
                return false;
            }
            if(!that.list.areaId){
                vm.$refs.prompt.toast("工作地点不能为空！");
                return false;
            }
            if(!that.list.introduce){
                vm.$refs.prompt.toast("职位描述不能为空！");
                return false;
            }
            if(!that.list.nature){
                vm.$refs.prompt.toast("工作性质不能为空！");
                return false;
            }
            if(!that.list.experience){
                vm.$refs.prompt.toast("工作经验不能为空！");
                return false;
            }
            if(!that.list.education){
                vm.$refs.prompt.toast("学历不能为空！");
                return false;
            }
            if(!that.list.salary){
                vm.$refs.prompt.toast("薪资范围不能为空！");
                return false;
            }
            console.log(that.list);
            that.ajax({url:"/happyi/updateRecruitPositionDetailInfo",data:that.list,dataType:"json",method:"post"},function(b){
                if(b.json.code==0){
                    if(b.json.data){

                    }else{
                        that.getBack();
                    }
                }else{
                    vm.$refs.prompt.toast("保存信息失败！");
                }
            });
        },
        //删除职位
        deleteRecruitById:function(){
            var that=this;
            var data={
                loginUid:that.loginUid,
                recruitId:that.list.recruitId
            };
            this.ajax({url:"/happyi/deleteRecruitById",data:data,dataType:"json",method:"post"},function(b){
                if(b.json.code==0){
                    that.getBack();
                }else{
                    vm.$refs.prompt.toast("操作失败！");
                }
            });
        }
    }
    ,watch:{
        rCategorys:function(){
            this.$nextTick(function(){
                var that=this;
                var container = document.querySelectorAll('#screen .chooseCity-city');

                var expansion = null; //是否存在展开的list
                for(var i = 0; i < container.length; i++){
                    var x, y, X, Y, swipeX, swipeY;
                    container[i].addEventListener('touchstart', function(event) {
                        console.log(container)
                        x = event.changedTouches[0].pageX;
                        y = event.changedTouches[0].pageY;
                        swipeX = true;
                        swipeY = true ;
                        console.log("1111")
                        // if(vm.rCategorys){   //判断是否展开，如果展开则收起
                        //     expansion.className ="chooseCity-city clearfix";
                        // }
                    });
                    container[i].addEventListener('touchmove', function(event){
                        event.stopPropagation();
                        X = event.changedTouches[0].pageX;
                        Y = event.changedTouches[0].pageY;
                        // 左右滑动
                        if(swipeX && Math.abs(X - x) - Math.abs(Y - y) > 0){
                            // 阻止事件冒泡
                            event.stopPropagation();
                            if(X - x > 10){   //右滑
                                // event.preventDefault();
                                this.className = "chooseCity-city clearfix";    //右滑收起
                            }
                            if(x - X > 10){   //左滑
                                event.preventDefault();
                                this.className = "chooseCity-city clearfix swipeleft";   //左滑展开
                                // expansion = this;
                            }
                            swipeY = false;
                        }
                        // 上下滑动
                        if(swipeY && Math.abs(X - x) - Math.abs(Y - y) < 0) {
                            swipeX = false;
                        }
                    });
                }
            });
        },
        recruits:function(){

        }
    }
});

// 数据初始化
var yearData = function(callback) {
    callback(formatYear(vm.nowYear))
};
var monthData = function (year, callback) {
    callback(formatMonth());
};
var dateData = function (year, month, callback) {
    if (/^(1|3|5|7|8|10|12)$/.test(month)) {
        callback(formatDate(31));
    }
    else if (/^(4|6|9|11)$/.test(month)) {
        callback(formatDate(30));
    }
    else if (/^2$/.test(month)) {
        if (year % 4 === 0 && year % 100 !==0 || year % 400 === 0) {
            callback(formatDate(29));
        }
        else {
            callback(formatDate(28));
        }
    }
    else {
        throw new Error('month is illegal');
    }
};
function formatYear (nowYear) {
    var arr = [];
    for (var i = nowYear - 45; i <= nowYear; i++) {
        arr.push({
            id: i + '',
            value: i + '年'
        });
    }
    return arr;
}
function formatMonth () {
    var arr = [];
    for (var i = 1; i <= 12; i++) {
        arr.push({
            id: i + '',
            value: i + '月'
        });
    }
    return arr;
}
function formatDate (count) {
    var arr = [];
    for (var i = 1; i <= count; i++) {
        arr.push({
            id: i + '',
            value: i + '日'
        });
    }
    return arr;
}